﻿using System.Runtime.InteropServices;
using static scanner_tron.tronapi;

namespace scanner_tron
{
    internal class Program
    {
        static tronapi api = new tronapi();
        static long begintime;
        static long finishtime;
        static void Main(string[] args)
        {
            Console.WriteLine("Scanner for tron");
            ScanTask();
            if (System.IO.File.Exists("range.txt") == false)
            {
                Console.WriteLine("need a range.txt");
                return;
            }
            var lines = System.IO.File.ReadAllLines("range.txt");
            begintime = long.Parse(lines[0]);
            finishtime = long.Parse(lines[1]);
            DateTimeOffset timebegin = DateTimeOffset.FromUnixTimeMilliseconds(begintime);
            DateTimeOffset timeend = DateTimeOffset.FromUnixTimeMilliseconds(finishtime);
            Console.SetCursorPosition(0, 0);
            Console.WriteLine("down range = " + timebegin + " ---- " + timeend);

            while (true)
            {
                Console.ReadLine();
            }
        }
        #region index store
        static void get_trc10_timestamp(out long time, out long page)
        {
            try
            {
                var lines = System.IO.File.ReadAllLines("data/index_trc10.txt");
                try
                {
                    time = long.Parse(lines[0]);
                }
                catch
                {
                    time = 0;
                }
                try
                {
                    page = long.Parse(lines[1]);
                }
                catch
                {
                    page = 0;
                }
            }
            catch
            {
                time = 0;
                page = 0;
            }
        }
        static void set_trc10_timestamp(long time, long page)
        {
            System.IO.File.WriteAllText("data/index_trc10.txt", time.ToString() + "\n" + page + "\n");
        }
        static void get_trc20_timestamp(out long time, out long page)
        {
            try
            {
                var lines = System.IO.File.ReadAllLines("data/index_trc20.txt");
                try
                {
                    time = long.Parse(lines[0]);
                }
                catch
                {
                    time = 0;
                }
                try
                {
                    page = long.Parse(lines[1]);
                }
                catch
                {
                    page = 0;
                }
            }
            catch
            {
                time = 0;
                page = 0;
            }
        }
        static void set_trc20_timestamp(long time, long page)
        {
            System.IO.File.WriteAllText("data/index_trc20.txt", time.ToString() + "\n" + page + "\n");
        }
        #endregion
        static async void ScanTask()
        {
            if (System.IO.Directory.Exists("data") == false)
                System.IO.Directory.CreateDirectory("data");


            await api.Start();
            if (System.IO.File.Exists("data/addr_start.txt") == false)
            {
                System.IO.File.AppendAllLines("data/addr_start.txt", api.startAddress);
            }
            TaskGetTrc(tronapi.Mode.TRC10);
            TaskGetTrc(tronapi.Mode.TRC20);





        }
        static async void TaskGetTrc(tronapi.Mode mode)
        {
            long time1;
            long page1;
            if (mode == tronapi.Mode.TRC10)
            {
                get_trc10_timestamp(out time1, out page1);
            }
            else
            {
                get_trc20_timestamp(out time1, out page1);
            }

            if (time1 == 0)
                time1 = begintime;
            List<string> addrbuf = new List<string>();
            while (true)
            {
                if (time1 > finishtime)
                {
                    Console.SetCursorPosition(0, mode == tronapi.Mode.TRC10 ? 1 : 2);
                    Console.WriteLine(mode + " finish=" + DateTimeOffset.FromUnixTimeMilliseconds(time1));
                    break;
                }
                //30分钟一页都太大了，改15分钟
                long endtime1 = time1 + 5 * 60 * 1000;
                addrbuf.Clear();
                try
                {
                    await api.GetTrcAddr(mode, time1, endtime1, addrbuf, page1);
                }
                catch(Exception err)
                {
                    Console.SetCursorPosition(0, mode == tronapi.Mode.TRC10 ? 1 : 2);
                    Console.WriteLine("error:" + err.Message);
                    continue;
                }
                if (addrbuf.Count > 0)
                {
                    var filename = (mode == tronapi.Mode.TRC10 ? "data/addrtrc10_" : "data/addrtrc20_") + (time1 / (30L * 24 * 3600 * 1000)) + ".txt";
                    System.IO.File.AppendAllLines(filename, addrbuf);
                    if (addrbuf.Count < 100)
                    {
                        page1 = 0;
                        time1 = endtime1;
                    }
                    else
                    {
                        page1++;
                    }
                    Console.SetCursorPosition(0, mode == tronapi.Mode.TRC10 ? 1 : 2);
                    Console.WriteLine(mode + " data=" + DateTimeOffset.FromUnixTimeMilliseconds(time1) + " page=" + page1 + "    ");
                }
                else
                {
                    Console.SetCursorPosition(0, mode == tronapi.Mode.TRC10 ? 1 : 2);
                    Console.WriteLine(mode + " nodata=" + DateTimeOffset.FromUnixTimeMilliseconds(time1) + "    ");
                    time1 = endtime1;
                    page1 = 0;
                }

                if (mode == tronapi.Mode.TRC10)
                {
                    set_trc10_timestamp(time1, page1);
                }
                else
                {
                    set_trc20_timestamp(time1, page1);
                }
            }
        }
    }
}